# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.categoryDao import CategoryDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertCategory', methods=['POST'])
def insert_categoryNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'catName': request.json.get('catName', ''),
        'catPcode': request.json.get('catPcode', ''),
        'catCode': request.json.get('catCode', ''),
        'catStatus': request.json.get('catStatus', 0),
        'catSort': request.json.get('catSort', 99)
    }

    info = CategoryDao().insertCategory(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findCategoryById', methods=['POST'])
def find_category_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    catId = request.json.get('catId', '')
    info = CategoryDao().findCategoryById(catId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "No such data"}})


@app.route('/findCategoryByCondition', methods=['POST'])
def find_category_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)

    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = CategoryDao().findCategoryByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                        multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 0, "status": "No such data"}})


@app.route('/deleteCategoryByIds', methods=['POST'])
def delete_category_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = CategoryDao().deleteCatgory(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateCategoryById', methods=['POST'])
def update_category_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        catId = request.json['catId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = CategoryDao().selectCat(catId)
    info = {
        'catId': request.json['catId'],
        'catName': request.json.get('catName', oldInfo[1]),
        'catPcode': request.json.get('catPcode', oldInfo[2]),
        'catCode': request.json.get('catCode', oldInfo[3]),
        'catStatus': request.json.get('catStatus', oldInfo[4]),
        'catSort': request.json.get('catSort', oldInfo[5])
    }

    info = CategoryDao().updateCategory(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
